###############################################################################
# This file provides code for  
#
# Moore, Ryan T. and Nirmala Ravishankar. ``Who Loses in Direct Democracy?''.  
# Social Science Research, 41(3):646-656, May 2012.
#
# Loads 2000 to 2004 Data
###############################################################################

## LAT439 
wid439c1 <- c(1,5,2,1,2,6,1,1,1,1,1,1,1,4,1,1,4,10,11,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,6,1,1,1)
wid439c2 <- c(1, 5, 3, 1, 70)
wid439c3 <- c(1, 5, 1, 5, 5, 5, 58) 
lat439 <- read.fwf("data/lat439.dat", widths = list(wid439c1, wid439c2, wid439c3))
n439 <- c("region", "language", "prop1a", "prop22", "prop26", "ideology", "party", "gender", "age", "educ", "religion", "race", "grabbag1:grabbag6", "income")
which439 <- c("V4", "V7", "V10", "V11", "V12", "V28", "V29", "V30", "V31", "V32", "V33", "V35", "V36", "V37")
lat439s <-lat439[, which439]
names(lat439s) <- n439
rm(wid439c1, wid439c2, wid439c3, which439, n439, lat439)
## recode region, exclude Kern and CV:
lat439s$region[lat439s$region %in% 5] <- 3
lat439s$region[lat439s$region %in% 6] <- 4
lat439s$region[lat439s$region %in% 8] <- 5
lat439s <- lat439s[lat439s$region %in% 1:5, ]
## recode ideology 1-3:
lat439s$ideology[lat439s$ideology %in% 2] <- 1
lat439s$ideology[lat439s$ideology %in% 3] <- 2
lat439s$ideology[lat439s$ideology %in% c(4,5)] <- 3
## recode party so consistent with 449-513:
lat439s$party[lat439s$party %in% 2] <- 5
lat439s$party[lat439s$party %in% 3] <- 2
lat439s$party[lat439s$party %in% 5] <- 3
## recode race:
lat439s$race[lat439s$race %in% 6] <- 5

## LAT 449
wid449 <-c(1,5,1,1,1,9,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
lat449 <- read.fwf("data/lat449.dat", widths = wid449)
lat449 <- lat449[!(is.na(lat449$V4)), ]  ## no Nat Area == NA
lat449 <- lat449[lat449$V4 == 9, ]## just CA respondents; n=3474
## fix prop 38.  This appears to be 70% for, but that's backwards.
lat449$V8[lat449$V8 == 1] <- 4
lat449$V8[lat449$V8 == 2] <- 1
lat449$V8[lat449$V8 == 4] <- 2
n449 <- c("region", "prop38", "prop39", "ideology", "party", "gender", "age", "educ", "race", "marital", "religion", "occupation", "income")
which449 <- c("V5", "V8", "V11", "V33", "V34", "V35", "V36", "V37", "V38", "V39", "V44", "V46", "V47")
lat449s <-lat449[, which449]
names(lat449s) <- n449
rm(wid449, n449, which449, lat449)
## recode ideology 1-3:
lat449s$ideology[lat449s$ideology %in% 2] <- 1
lat449s$ideology[lat449s$ideology %in% 3] <- 2
lat449s$ideology[lat449s$ideology %in% c(4,5)] <- 3
## recode race:
lat449s$race[lat449s$race %in% 6] <- 5

## LAT 468
wid468 <-c(7,7,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,2,7,7)
lat468 <- read.fwf("data/lat468.dat", widths = wid468)
n468 <- c("region", "party", "prop40", "prop45", "ideology", "gender", "age", "educ", "income", "race")
which468 <- c("V3","V18","V19","V21","V48","V49","V50","V54","V60","V67")
lat468s <- lat468[, which468]
names(lat468s) <- n468
rm(wid468, n468, which468, lat468)
## recode ideology:
lat468s$ideology[lat468s$ideology %in% "& "] <- NA
lat468s$ideology <- as.integer(lat468s$ideology)-1
## recode party:
lat468s$party[lat468s$party %in% "& "] <- NA
lat468s$party <- as.integer(lat468s$party)-1
## recode gender:
lat468s$gender[lat468s$gender %in% "& "] <- NA
lat468s$gender <- as.integer(lat468s$gender)-1
## recode age:
lat468s$age[lat468s$age %in% "& "] <- NA
lat468s$age <- as.integer(lat468s$age)-1
lat468s$age[lat468s$age %in% 4] <- 3
lat468s$age[lat468s$age %in% 5:8] <- 4
lat468s$age[lat468s$age %in% 9] <- 5
## recode educ:
lat468s$educ[lat468s$educ %in% "& "] <- NA
lat468s$educ <- as.integer(lat468s$educ)-1
## recode race:
lat468s$race[lat468s$race %in% "& "] <- NA
lat468s$race <- as.integer(lat468s$race)-1
lat468s$race[lat468s$race %in% 6] <- 5
## recode income:
lat468s$income[lat468s$income %in% "& "] <- NA
lat468s$income <- as.integer(lat468s$income)-1

## LAT 478
wid478 <-c(7,7,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,4,2)
lat478 <-read.fwf("data/lat478.dat", widths = wid478)
n478 <-c("region","language","prop49","prop52","party","ideology","gender","age","educ","income","sexual","race")
which478 <-c("V3","V4","V8","V10","V20","V21","V23","V46","V50","V58","V62","V64")
lat478s <-lat478[,which478]
names(lat478s) <- n478
rm(wid478, n478, which478, lat478)
## recode ideology 1-3:
lat478s$ideology[lat478s$ideology %in% "& "] <- NA
lat478s$ideology <- as.integer(lat478s$ideology)-1
lat478s$ideology[lat478s$ideology %in% 2] <- 1
lat478s$ideology[lat478s$ideology %in% 3] <- 2
lat478s$ideology[lat478s$ideology %in% c(4,5)] <- 3
## recode party:
lat478s$party[lat478s$party %in% "& "] <- NA
lat478s$party <- as.integer(lat478s$party)-1
## recode gender:
lat478s$gender[lat478s$gender %in% "& "] <- NA
lat478s$gender <- as.integer(lat478s$gender)-1
## recode age:
lat478s$age[lat478s$age %in% "&  "] <- NA
lat478s$age <- as.integer(lat478s$age)-1
lat478s$age[lat478s$age %in% 4] <- 3
lat478s$age[lat478s$age %in% 5:8] <- 4
lat478s$age[lat478s$age %in% 9] <- 5
## recode educ:
lat478s$educ[lat478s$educ %in% "& "] <- NA
lat478s$educ <- as.integer(lat478s$educ)-1
## recode race:
lat478s$race[lat478s$race %in% "& "] <- NA
lat478s$race <- as.integer(lat478s$race)-1
lat478s$race[lat478s$race %in% 6] <- 5
## recode income:
lat478s$income[lat478s$income %in% "& "] <- NA
lat478s$income <- as.integer(lat478s$income)-1

## LAT 490
wid490 <-c(7,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,2,2,2,2,2,2,2,2,2)
lat490 <- read.fwf("data/lat490.dat", widths = wid490)
n490 <-c("region", "language", "prop53", "prop54", "party", "ideology", "gender", "age", "educ", "income", "race")
which490 <- c("V3","V4","V9","V11","V27","V28","V37","V48","V49","V54","V57")
lat490s <- lat490[, which490]
names(lat490s) <- n490
rm(wid490, n490, which490, lat490)
## recode ideology 1-3:
lat490s$ideology[lat490s$ideology %in% "& "] <- NA
lat490s$ideology <- as.integer(lat490s$ideology)-1
lat490s$ideology[lat490s$ideology %in% 2] <- 1
lat490s$ideology[lat490s$ideology %in% 3] <- 2
lat490s$ideology[lat490s$ideology %in% c(4,5)] <- 3
## recode party:
lat490s$party[lat490s$party %in% "& "] <- NA
lat490s$party <- as.integer(lat490s$party)-1
## recode gender:
lat490s$gender[lat490s$gender %in% "& "] <- NA
lat490s$gender <- as.integer(lat490s$gender)-1
## recode age:
lat490s$age[lat490s$age %in% "&  "] <- NA
lat490s$age <- as.integer(lat490s$age)-1
lat490s$age[lat490s$age %in% 4] <- 3
lat490s$age[lat490s$age %in% 5:8] <- 4
lat490s$age[lat490s$age %in% 9] <- 5
## recode educ:
lat490s$educ[lat490s$educ %in% "& "] <- NA
lat490s$educ <- as.integer(lat490s$educ)-1
## recode race:
lat490s$race[lat490s$race %in% "& "] <- NA
lat490s$race <- as.integer(lat490s$race)-1
lat490s$race[lat490s$race %in% 6] <- 5
## recode income:
lat490s$income[lat490s$income %in% "& "] <- NA
lat490s$income <- as.integer(lat490s$income)-1

## LAT 500
wid500 <-c(7,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,2,2,2,2,2,2,2,2,7,7)
lat500 <- read.fwf("data/lat500.dat", widths = wid500)
n500 <-c("region", "language", "prop55", "prop56", "prop57", "prop58", "gender", "ideology", "party", "age", "educ", "income", "sexual", "race")
which500 <-c("V4","V6", "V11","V13","V15","V17", "V22","V45","V47","V49","V50", "V53","V54","V56")
lat500s <- lat500[, which500]
names(lat500s) <- n500
rm(wid500, n500, which500, lat500)
## recode ideology 1-3:
lat500s$ideology[lat500s$ideology %in% "& "] <- NA
lat500s$ideology <- as.integer(lat500s$ideology)-1
lat500s$ideology[lat500s$ideology %in% 2] <- 1
lat500s$ideology[lat500s$ideology %in% 3] <- 2
lat500s$ideology[lat500s$ideology %in% c(4,5)] <- 3
## recode party:
lat500s$party[lat500s$party %in% "& "] <- NA
lat500s$party <- as.integer(lat500s$party)-1
## recode gender:
lat500s$gender[lat500s$gender %in% "& "] <- NA
lat500s$gender <- as.integer(lat500s$gender)-1
## recode age:
lat500s$age[lat500s$age %in% "&  "] <- NA
lat500s$age <- as.integer(lat500s$age)-1
lat500s$age[lat500s$age %in% 4] <- 3
lat500s$age[lat500s$age %in% 5:8] <- 4
lat500s$age[lat500s$age %in% 9] <- 5
## recode educ:
lat500s$educ[lat500s$educ %in% "& "] <- NA
lat500s$educ <- as.integer(lat500s$educ)-1
## recode race:
lat500s$race[lat500s$race %in% "& "] <- NA
lat500s$race <- as.integer(lat500s$race)-1
lat500s$race[lat500s$race %in% 6] <- 5
## recode income:
lat500s$income[lat500s$income %in% "& "] <- NA
lat500s$income <- as.integer(lat500s$income)-1

## LAT 513
wid513 <- c(7,7,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,7,2)
lat513 <- read.fwf("data/lat513.dat", widths = wid513)
lat513 <- lat513[lat513$V3 == 9, ]## just CA respondents; n=3357
n513 <- c("region", "prop68", "prop70", "prop71", "prop72", "party", "gender", "race", "ideology", "age", "educ", "occupation", "income", "sexual")
which513 <- c("V4","V11","V13", "V15","V17", "V25","V26","V27", "V44","V45","V46", "V56","V57","V59")
lat513s <- lat513[, which513]
names(lat513s) <- n513
rm(wid513, n513, which513, lat513)
## recode region; 513 excludes Orange Cty:
lat513s$region[lat513s$region %in% "& "] <- NA
lat513s$region <- as.integer(lat513s$region)-1
lat513s$region[lat513s$region %in% 4] <- 5
lat513s$region[lat513s$region %in% 3] <- 4
lat513s$region[lat513s$region %in% 2] <- 3
## recode ideology 1-3:
lat513s$ideology[lat513s$ideology %in% "& "] <- NA
lat513s$ideology <- as.integer(lat513s$ideology)-1
lat513s$ideology[lat513s$ideology %in% 2] <- 1
lat513s$ideology[lat513s$ideology %in% 3] <- 2
lat513s$ideology[lat513s$ideology %in% c(4,5)] <- 3
## recode party:
lat513s$party[lat513s$party %in% "& "] <- NA
lat513s$party <- as.integer(lat513s$party)-1
## recode gender:
lat513s$gender[lat513s$gender %in% "& "] <- NA
lat513s$gender <- as.integer(lat513s$gender)-1
## recode age:
lat513s$age[lat513s$age %in% "& "] <- NA
lat513s$age <- as.integer(lat513s$age)-1
## recode educ:
lat513s$educ[lat513s$educ %in% "& "] <- NA
lat513s$educ <- as.integer(lat513s$educ)-1
## recode race:
lat513s$race[lat513s$race %in% "& "] <- NA
lat513s$race <- as.integer(lat513s$race)-1
## recode income:
lat513s$income[lat513s$income %in% "& "] <- NA
lat513s$income <- as.integer(lat513s$income)-1

reshapeData <- function(dat, ampersand = TRUE, margin = NULL, year = 2000, type = NULL, outcome = NULL){
	k <- ncol(dat)
	which.pp <- grep("prop", names(dat))
	names.pp <- names(dat)[which.pp]
	## Set up first prop for this year's data:
	tmp.dat <- cbind(dat[, (1:k)[-which.pp]], vote=dat[, which.pp[1]], prop = NA, margin = margin[1], outcome = outcome[1], winner = NA, type = type[1]) 
	tmp.dat[, "prop"] <- substring(names.pp[1], 5, nchar(names.pp[1]))
	## Iterate through remaining props for this year:
	if(length(which.pp) > 1){
		for(i in 2:length(which.pp)){
			tmp.dat.add <- cbind(dat[, (1:k)[-which.pp]], vote=dat[, which.pp[i]], prop = NA, margin = margin[i], outcome = outcome[i], winner = NA, type = type[i])
			tmp.dat.add[, "prop"] <- substring(names.pp[i], 5, nchar(names.pp[i]))
			tmp.dat <- rbind(tmp.dat, tmp.dat.add)
		}
	}
	tmp.dat$year <- year
	
	if(ampersand == TRUE){
		tmp.dat <- tmp.dat[!(tmp.dat$vote == "3 "), ] ## remove explicit Did Not Vote's
		tmp.dat$vote[tmp.dat$vote == "& "] <- NA
		tmp.dat$vote <- as.integer(tmp.dat$vote)-1
	}else{
		tmp.dat <- tmp.dat[(tmp.dat$vote %in% c(1,2,NA)), ] ## remove explicit Did Not Vote's
	}

  tmp.dat$winner <- as.numeric(tmp.dat$vote == tmp.dat$outcome)
	return(tmp.dat)
}

lat439 <- reshapeData(lat439s, ampersand = FALSE, margin = c(28.89, 22.71, 2.54), type = c("Morality", "Gay", "Education"), outcome = c(1,1,2))
lat449 <- reshapeData(lat449s, ampersand = FALSE, margin = c(41.08, 6.62), type = c("Education", "Education"), outcome = c(2, 1))
lat468 <- reshapeData(lat468s, margin = c(13.67, 15.31), year = 2002, type = c("Environment", "Elections"), outcome = c(1, 2))
lat478 <- reshapeData(lat478s, margin = c(13.23, 18.11), year = 2002, type = c("Education", "Elections"), outcome = c(1, 2))
lat490 <- reshapeData(lat490s, margin = c(27.55, 27.60), year = 2003, type = c("Fiscal", "Race"), outcome = c(2, 2))
lat500 <- reshapeData(lat500s, margin = c(1.71, 31.36, 26.66, 42.25), year = 2004, type = c("Education", "Fiscal", "Fiscal", "Fiscal"), outcome = c(1,2,1,1))
lat513 <- reshapeData(lat513s, margin = c(67.57, 52.53, 18.10, 1.56), year = 2004, outcome = c(2,2,1,2), type = c("Morality", "Morality", "Health", "Health"))
rm(reshapeData, lat439s, lat449s, lat468s, lat478s, lat490s, lat500s, lat513s)
